// 引入请求
import { getHotMovieList } from '@/api'
import { ref } from 'vue'

export function List() {
  interface IParams {
    cityId: number
    pageNum: number
    pageSize: number
    type: number
    k: number
  }

  const params: IParams = {
    cityId: 110100,
    pageNum: 1,
    pageSize: 10,
    type: 1,
    k: 2771289
  }
  // 定义返回值接收返回过来的数据
  const list = ref<any[]>([])
  const loading = ref(false)
  const finished = ref(false)
  const to = ref(20)

  // // 自调用
  // ;(async function () {
  //   const {
  //     data: {
  //       data: { films }
  //     }
  //   } = await getHotMovieList(params)
  //   list.value = films
  //   console.log(list)
  // })()
  const onLoad = async () => {
    const {
      data: {
        data: { films, total }
      }
    } = await getHotMovieList(params)

    list.value = [...list.value, ...films]
    loading.value = false
    params.pageNum++
    to.value = total
    if (list.value.length >= to.value) {
      finished.value = true
    }
  }
  return { onLoad, loading, finished, list }
}
